﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Framework.Logic.Server;

namespace Framework.Forms
{
    public partial class ConsoleChat : Form
    {
        public ConsoleChat(IMCServer mcServer)
        {
            InitializeComponent();
            this.mcServer = mcServer;
         
        }

        public IMCServer mcServer { get; set; }

        private void ConsoleChat_Load(object sender, EventArgs e)
        {
            txtConsoleText.BackColor = Color.White;
            foreach (var item in mcServer.Output)
            {
                txtConsoleText.Text = txtConsoleText.Text + item + Environment.NewLine;
            }

            mcServer.NewOutputLine += new EventHandler(mcServer_NewOutputLine);
            txtConsoleText.SelectionStart = txtConsoleText.Text.Length;
            txtConsoleText.ScrollToCaret();
            txtConsoleText.Refresh();
        }

        void mcServer_NewOutputLine(object sender, EventArgs e)
        {
            this.Invoke((MethodInvoker)delegate
            {
                txtConsoleText.Text = txtConsoleText.Text + sender.ToString() + Environment.NewLine;
            });
        }

        private void ConsoleChat_FormClosing(object sender, FormClosingEventArgs e)
        {
            mcServer.NewOutputLine -= new EventHandler(mcServer_NewOutputLine);
        }

        private void txtConsoleText_TextChanged(object sender, EventArgs e)
        {
            txtConsoleText.SelectionStart = txtConsoleText.Text.Length;
            txtConsoleText.ScrollToCaret();
            txtConsoleText.Refresh();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            mcServer.Send(txtCommandText.Text);
            txtCommandText.Clear();
            txtCommandText.Focus();
        }

        private void txtCommandText_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSend_Click(sender, e);
            }
        }
    }
}
